package com.telecom.controller.admin;

import com.telecom.common.convention.result.PageDTO;
import com.telecom.common.convention.result.Result;
import com.telecom.pojo.dto.admin.meal.AddMealDTO;
import com.telecom.pojo.dto.admin.meal.ModifyMealDTO;
import com.telecom.pojo.dto.admin.meal.AdminMealPageQueryRequestDTO;
import com.telecom.pojo.vo.admin.setmeal.AdminMealDetailVO;
import com.telecom.pojo.vo.admin.setmeal.MealPageVO;
import com.telecom.service.MealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;

import java.util.List;

/**
 * 管理端套餐相关接口
 */
@RestController
@RequestMapping("/admin/meal")
@Slf4j
public class AdminMealController {

    @Resource
    private MealService mealService;

    /**
     * 新增套餐
     * 
     * @param addMealDTO 套餐信息
     * @return 新增结果
     */
    @PostMapping
    public Result<Void> addMeal(@RequestBody @Valid AddMealDTO addMealDTO) {
        mealService.addMeal(addMealDTO);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * 
     * @param ids id列表
     * @return 删除结果
     */
    @DeleteMapping
    public Result<Void> deleteMeals(@RequestParam List<Long> ids) {
        mealService.deleteMeals(ids);
        return Result.success();
    }

    /**
     * 根据套餐id修改套餐
     * 
     * @param modifyMealDTO 套餐信息
     * @return 修改结果
     */
    @PutMapping
    public Result<Void> updateMeal(@RequestBody @Valid ModifyMealDTO modifyMealDTO) {
        mealService.updateMeal(modifyMealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐详细
     * 
     * @param id 套餐id
     * @return 套餐详情
     */
    @GetMapping("/{id}")
    public Result<AdminMealDetailVO> getMealById(@PathVariable @Positive(message = "套餐ID必须为正数") Long id) {
        AdminMealDetailVO mealDetail = mealService.getMealById(id);
        return Result.success(mealDetail);
    }

    /**
     * 套餐按日期分页查询
     * 
     * @param queryRequest 查询参数
     * @return 套餐列表分页结果
     */
    @GetMapping("/page")
    public Result<PageDTO<MealPageVO>> page(@Valid AdminMealPageQueryRequestDTO queryRequest) {
        PageDTO<MealPageVO> pageResult = mealService.page(queryRequest);
        return Result.success(pageResult);
    }

    /**
     * 套餐起售、停售
     * 
     * @param status 套餐状态，1表示起售，0表示停售
     * @param id 套餐id
     * @return 操作结果
     */
    @PostMapping("/status/{status}")
    public Result<Void> updateStatus(@PathVariable String status,
                                   @RequestParam @Positive(message = "套餐ID必须为正数") Long id) {
        Boolean statusValue = "1".equals(status);
        mealService.updateStatus(id, statusValue);
        return Result.success();
    }
}